package com.sanss.log_plat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Controller
public class OiddLogController {

    private RestTemplate restTemplate = new RestTemplate();

    @Autowired
    private ConcurrentHashMap db;

    @RequestMapping("/log")
    @ResponseBody
    public String listError(HttpServletResponse response){
        String reqString =
                        "{\n" +
                                "  \"query\": {\n" +
                                "    \"range\": {\n" +
                                "      \"@timestamp\": {\n" +
                                "        \"gt\":  \"2017-11-20 14:44:10 \",\n" +
                                "        \"lt\":   \"2017-11-20 14:44:10 \"\n" +
                                "      }\n" +
                                "    }\n" +
                                "  }\n" +
                                "}";

        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(reqString,headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                "http://172.16.53.132:9200/catalina.out-cj01-2017.11.20/_search?pretty", HttpMethod.POST,
                entity, String.class);

        System.out.println(responseEntity.getBody());
        if(responseEntity.getStatusCodeValue() == 200) {
//            System.out.println(responseEntity.);
        }
        return responseEntity.getBody();
    }

    @RequestMapping("/showindex")
    @ResponseBody
    public String showIndexs(HttpServletResponse response){
        String reqString =
                "{}";

        HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(reqString,headers);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
//                "http://172.16.53.132:9200/_cat/indices?v", String.class);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "http://localhost:9200/_cat/indices?v", String.class);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
//                "http://172.16.53.132:9200/catalina.out-cj01-2017.11.20/_mapping?pretty", String.class);
        System.out.println(responseEntity.getBody());
        if(responseEntity.getStatusCodeValue() == 200) {
//            System.out.println(responseEntity.);
        }
        return responseEntity.getBody();
    }


    @RequestMapping("/setdb")
    @ResponseBody
    public String setDB(){
        db.put("name","mydb");
        return "ok";
    }

    @RequestMapping("/getdb")
    @ResponseBody
    public String getDB(){
        return (String)db.get("name");
    }



}
